package org.vision.contactsmanager.ui;

import java.util.List;

import org.vision.contactsmanager.R;
import org.vision.contactsmanager.bean.MyContacts;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 搜索界面列表的数据适配器
 * @author dengjie
 *
 */
public class SearchContactsAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<MyContacts> itemList;
    private Context context;

    /**
     * 构造方法
     * @param context 启动的上下文
     * @param itemList 传入LIST的数据
     * @param onClickListener 绑定LIST的监听器
     */
    public SearchContactsAdapter(Context context, List<MyContacts> itemList) {
        mInflater = LayoutInflater.from(context);
        this.itemList = itemList;
        this.context = context;
    }

    public List<MyContacts> getItemList() {
        return itemList;
    }

    public void setItemList(List<MyContacts> itemList) {
        this.itemList = itemList;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //这个比较特殊,adapter是在页面变化的时候,重新获取当前页面的数据
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();

        MyContacts mc = itemList.get(position);
        convertView = mInflater.inflate(R.layout.searchchildlayout, null);
        //绑定UI
        holder.contactsCId = (TextView) convertView.findViewById(R.id.contactsCId);
        holder.contactsCId.setText(mc.getContactsContractId());

        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.name.setText(mc.getName());

        holder.description = (TextView) convertView.findViewById(R.id.description);
        holder.description.setTextKeepState(mc.getDescription());

        final String phoneNumber = mc.getTelPhone();
        holder.phoneNumberTv = (TextView) convertView.findViewById(R.id.phoneNumber);
        holder.phoneNumberTv.setText(phoneNumber);

        holder.mycursor = (ImageView) convertView.findViewById(R.id.myCursor);
        //ListView联系人列表后的短信图标ICON的点击事件
        holder.mycursor.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("smsto:" + phoneNumber);
                Intent it = new Intent(Intent.ACTION_SENDTO, uri);
                it.putExtra("sms_body", "");
                context.startActivity(it);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView contactsCId;
        TextView name;
        TextView description;
        TextView phoneNumberTv;
        ImageView mycursor;
    }

}